function Vue(options) {
  this.$data = this._data = options.data
  observer(this.$data)
  Object.keys(this.$data).forEach((key) => {
    this.proxy(key)
  })
  Compile(options.el, this)
}

//数据代理
Vue.prototype = {
  proxy: function (key) {
    Object.defineProperty(this, key, {
      get() {
        return this.$data[key]
      },
      set(val) {
        this.$data[key] = val
      },
    })
  },
}

//数据劫持
function observer(obj) {
  if (!obj || typeof obj !== 'object') return
  Object.keys(obj).forEach((key) => {
    var originValue = obj[key]
    observer(originValue)
    Object.defineProperty(obj, key, {
      get() {
        return originValue
      },
      set(val) {
        originValue = val
        observer(originValue)
      },
    })
  })
}

//数据编译
function Compile(el, vm) {
  if (typeof el === 'string') {
    vm.$el = document.querySelector(el)
  } else {
    vm.$el = el
  }
}
